package org.xhy.sniffer.plugins.thread;

import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.xhy.sniffer.plugin.ClassEnhancePluginDefine;
import org.xhy.sniffer.interceptor.ConstructorMethodsInterceptorPoint;
import org.xhy.sniffer.interceptor.InstanceMethodsInterceptorPoint;
import org.xhy.sniffer.interceptor.StaticMethodsInterceptorPoint;
import org.xhy.sniffer.match.ClassMatch;
import org.xhy.sniffer.match.HierarchyMatch;

import static net.bytebuddy.matcher.ElementMatchers.named;

/**
 * @description:
 * @gitee: https://gitee.com/XhyQAQ
 * @copyright: B站: https://space.bilibili.com/152686439
 * @Author: Xhy
 * @CreateTime: 2024-07-21 21:13
 */
public class ThreadInstrumentation extends ClassEnhancePluginDefine {
    private static final String THREAD_CALLS = "java.lang.Runnable";
    private static final String THREAD_START_METHOD = "start";
    private static final String INTERCEPTOR = "org.xhy.apm.plugins.thread.interceptor.ThreadInterceptor";

    @Override
    protected ClassMatch enhanceClass() {
        return HierarchyMatch.byHierarchyMatch(THREAD_CALLS);
    }

    @Override
    protected InstanceMethodsInterceptorPoint[] getInstanceMethodsInterceptorPoints() {
        return new InstanceMethodsInterceptorPoint[]{
                new InstanceMethodsInterceptorPoint() {
                    @Override
                    public ElementMatcher<MethodDescription> getMethodsMatcher() {
                        return (named(THREAD_START_METHOD));
                    }

                    @Override
                    public String getMethodsInterceptor() {
                        return INTERCEPTOR;
                    }
                }
        };
    }

    @Override
    protected ConstructorMethodsInterceptorPoint[] getConstructorMethodsInterceptorPoints() {
        return new ConstructorMethodsInterceptorPoint[0];
    }

    @Override
    protected StaticMethodsInterceptorPoint[] getStaticMethodsInterceptorPoints() {
        return new StaticMethodsInterceptorPoint[0];
    }
}
